/**
 * yiqihao(一起好商务股份有限公司) copyright 
 */
package com.p2p.front.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.p2p.base.exception.BizException;
import com.p2p.front.dal.dao.ImageStoreDao;
import com.p2p.front.dal.model.ImageStore;
import com.p2p.front.enums.PhotoType;
import com.p2p.front.service.ImageStoreService;

/**
 *
 * @author leiruiqi@yiqihao.com
 *
 * 2015年3月16日 下午3:27:53
 */

@Service("imageStoreService")
public class ImageStoreServiceImpl implements ImageStoreService{

	@Resource
	private ImageStoreDao imageStoreDao;
	
	@Override
	public void create(ImageStore imageStore) {
		
		fillDefaultImageStoreColumns(imageStore);
		
		if(checkCreateImageStoreColumns(imageStore)){
			imageStoreDao.create(imageStore);
		}else{
			throw new BizException(null,"checkCreateImageStoreColumns false");
		}
		
	}
	
	public void saveImageFile(){
		
	}
	
	@Override
	public boolean updateStatus(Long id, Integer status) {
		return 1==imageStoreDao.updateStatus(id, status);
	}

	@Override
	public boolean updateSortNo(Long id, Integer sortNo) {
		return 1==imageStoreDao.updateSortNo(id, sortNo);
	}

	@Override
	public List<ImageStore> queryBizAndStatus(ImageStore imageStore) {
		return imageStoreDao.queryBizAndStatus(imageStore);
	}

	private void fillDefaultImageStoreColumns(ImageStore imageStore){
		if(imageStore.getStatus()==null){
			imageStore.setStatus(1);
		}
		if(imageStore.getPathType()==null){
			imageStore.setPathType(1);
		}
		if(imageStore.getSortNo()==null){
			imageStore.setSortNo(0);
		}
	}
	
	private boolean checkCreateImageStoreColumns(ImageStore imageStore){
		if(imageStore.getBizId()!=null
				&&imageStore.getBizType()!=null
				&&imageStore.getStatus()!=null
				&&imageStore.getSortNo()!=null
				&&imageStore.getPathType()!=null
				&&StringUtils.isNotBlank(imageStore.getPath())
				){
			return true;
		}
		return false;
	}
	@Override
	public List<ImageStore> queryBizAndStatus(Long bizId, Integer bizType,Integer status) {
		ImageStore imageStore = new ImageStore();
		imageStore.setBizId(bizId);
		imageStore.setBizType(bizType);
		imageStore.setStatus(status);
		//imageStore.setSortNo(0);
		imageStore.setPathType(1);
		
		return imageStoreDao.queryBizAndStatus(imageStore);
	}

	@Override
	public void createDefault(String path, Long bizId,PhotoType photoType,String remark) {
		ImageStore imageStore = new ImageStore();
		imageStore.setBizId(bizId);
		imageStore.setBizType(photoType.getCode());
		imageStore.setStatus(1);
		imageStore.setSortNo(0);
		imageStore.setPathType(1);
		imageStore.setPath(path);
		imageStore.setRemark(remark);
		this.create(imageStore);
		
	}

	@Override
	public boolean updateUrl(Long id, String url) {
		return 1==imageStoreDao.updateUrl(id, url);
	}

	
}
